<?php
class DisposalsController extends AppController {

	var $name = 'Disposals';

	function index() {
		$this->Disposal->recursive = 0;
		$this->set('disposals', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid disposal', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('disposal', $this->Disposal->read(null, $id));
	}

	function add($id = null) {
		if (!empty($this->data)) {
			$this->data['Disposal']['disposal_date'] = date("Y-m-d", strtotime($this->data['Disposal']['disposal_date']));
			$this->Disposal->create();
			if ($this->Disposal->save($this->data)) {
				$this->Session->setFlash(__('The disposal has been saved', true));
				$this->redirect(array('controller'=>'items','action' => 'view',$this->data['Disposal']['item_id']));
			} else {
				$this->Session->setFlash(__('The disposal could not be saved. Please, try again.', true));
			}
		}
		$items = $this->Disposal->Item->find('list');
		$this->set(compact('items','id'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid disposal', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Disposal->save($this->data)) {
				$this->Session->setFlash(__('The disposal has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The disposal could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Disposal->read(null, $id);
		}
		$items = $this->Disposal->Item->find('list');
		$this->set(compact('items'));
	}

	function delete($id = null) {
		
		$this->Disposal->recursive = -1;
		$item = $this->Disposal->findById($id);
		
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for disposal', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Disposal->delete($id)) {
			$this->Session->setFlash(__('Disposal deleted', true));
			$this->redirect(array('controller'=>'items','action'=>'view',$item['Disposal']['item_id']));
		}
		$this->Session->setFlash(__('Disposal was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
